<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/4/19
 * Time: 11:43
 */

namespace App\Model;


use Hyperf\DbConnection\Db;

class GameLevel extends Model
{
    protected $table = 'game_level';
    protected $fillable = ['id', 'game_id', 'level_name', 'level', 'cpns_id', 'type', 'chance_per'];
    public $timestamps = false;

    public static $game_id = 6;

    // 获取游戏奖励关卡记录
    public static function getRecords($where, $order, $by = 'desc')
    {
        try {
            $data = Db::table('game_level')
                ->join('coupons', 'game_level.cpns_id', '=', 'coupons.cpns_id')
                ->where($where)
                ->select('game_level.level', 'game_level.level_name', 'coupons.cpns_id as cpns_id', 'game_level.chance_per', 'coupons.show_pre', 'coupons.cpns_hash', 'coupons.cpns_prefix', 'coupons.cpns_key', 'coupons.cpns_expire', 'coupons.show_apply', 'coupons.cpns_name')
                ->orderBy($order, $by)
                ->get();
            return ['code' => 0, 'msg' => '获取成功！', 'data' => $data->toArray()];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    // 获取所有奖励
    public static function getAll()
    {
        try {
            $where = [
                ['game_level.type', '=', 1],
                ['game_level.game_id', '=', self::$game_id]
            ];
            $res = self::query()->join('coupons', 'game_level.cpns_id', '=', 'coupons.cpns_id')
                ->where($where)
                ->select('game_level.level', 'game_level.chance_per', 'coupons.show_pre', 'coupons.cpns_name', 'coupons.show_conditions', 'coupons.cpns_expire')
                ->orderBy('level', 'asc')
                ->get();
            if (!empty($res)) {
                return ['code' => 0, 'data' => $res->toArray()];
            }
            return ['code' => 2, 'msg' => '暂无配置'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function getPrize($stage)
    {
        try {
            $where = [
                ['game_level.type', '=', 1],
                ['game_level.game_id', '=', self::$game_id],
                ['game_level.level', '=', $stage],
            ];
            $res = self::query()->join('coupons', 'game_level.cpns_id', '=', 'coupons.cpns_id')
                ->where($where)
                ->select('game_level.id', 'game_level.level', 'game_level.level_name', 'coupons.cpns_id as cpns_id', 'game_level.chance_per', 'coupons.show_pre', 'coupons.cpns_hash', 'coupons.cpns_prefix', 'coupons.cpns_key', 'coupons.cpns_expire', 'coupons.show_apply', 'coupons.cpns_name', 'coupons.show_conditions')
                ->get();
            if (!empty($res)) {
                return ['code' => 0, 'data' => $res->toArray()];
            }
            return ['code' => 2, 'msg' => '暂无配置'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}